home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / ipc / sub_servmesg.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  58 lines

  1. #include    <stdio.h>
  2. #include    "mesg.h"
  3.  
  4. extern int    errno;
  5.  
  6. Mesg    mesg;
  7.  
  8. server(ipcreadfd, ipcwritefd)
  9. int    ipcreadfd;
  10. int    ipcwritefd;
  11. {
  12.     int    n, filefd;
  13.     char    errmesg[256], *sys_err_str();
  14.  
  15.     /*
  16.      * Read the filename message from the IPC descriptor.
  17.      */
  18.  
  19.     mesg.mesg_type = 1L;
  20.     if ( (n = mesg_recv(ipcreadfd, &mesg)) <= 0)
  21.         err_sys("server: filename read error");
  22.     mesg.mesg_data[n] = '\0';    /* null terminate filename */
  23.  
  24.     if ( (filefd = open(mesg.mesg_data, 0)) < 0) {
  25.         /*
  26.          * Error.  Format an error message and send it back
  27.          * to the client.
  28.          */
  29.  
  30.         sprintf(errmesg, ": can't open, %s\n", sys_err_str());
  31.         strcat(mesg.mesg_data, errmesg);
  32.         mesg.mesg_len = strlen(mesg.mesg_data);
  33.         mesg_send(ipcwritefd, &mesg);
  34.  
  35.     } else {
  36.         /*
  37.          * Read the data from the file and send a message to
  38.          * the IPC descriptor.
  39.          */
  40.  
  41.         while ( (n = read(filefd, mesg.mesg_data, MAXMESGDATA)) > 0) {
  42.             mesg.mesg_len = n;
  43.             mesg_send(ipcwritefd, &mesg);
  44.         }
  45.         close(filefd);
  46.  
  47.         if (n < 0)
  48.             err_sys("server: read error");
  49.     }
  50.  
  51.     /*
  52.      * Send a message with a length of 0 to signify the end.
  53.      */
  54.  
  55.     mesg.mesg_len = 0;
  56.     mesg_send(ipcwritefd, &mesg);
  57. }
  58.